import {
  Body,
  Controller,
  Get,
  ParseIntPipe,
  Post,
  Req,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import { CreateUserDto } from './dto/create-user.dto';
import { UserService } from './user.service';
import { Request } from 'express';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  async login(@Body() createUserDto: CreateUserDto) {
    return await this.userService.login(createUserDto);
  }
  @Post('avatar')
  @UseGuards(AuthGuard('jwt'))
  @UseInterceptors(FileInterceptor('avatar'))
  updateAvatar(@UploadedFile() file: any, @Req() request: Request) {
    let openId = (request as any).user.id;
    return this.userService.updateAvatar(file, openId);
  }
  @Post('nickName')
  @UseGuards(AuthGuard('jwt'))
  updateNickName(@Body('nickName') nickName: string, @Req() request: Request) {
    let openId = (request as any).user.id;
    return this.userService.updateNickName(nickName, openId);
  }

  @Post('gender')
  @UseGuards(AuthGuard('jwt'))
  updateGender(
    @Body('gender', ParseIntPipe) gender: number,
    @Req() request: Request,
  ) {
    let openId = (request as any).user.id;
    return this.userService.updateGender(gender, openId);
  }

  @Get()
  async findAll() {
    return await this.userService.findAll();
  }
}
